package com.zjb.demo.common.interceptor;

import com.zjb.demo.common.domain.response.AjaxResult;
import com.zjb.demo.common.exception.AppException;
import org.noear.solon.annotation.Component;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Handler;
import org.noear.solon.core.route.RouterInterceptor;
import org.noear.solon.core.route.RouterInterceptorChain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 庄佳彬
 * @since 2023/12/24 13:36
 */
@Component
public class AppRouterInterceptor implements RouterInterceptor {
    private static final Logger log = LoggerFactory.getLogger(AppRouterInterceptor.class);

    @Override
    public void doIntercept(Context ctx, Handler mainHandler, RouterInterceptorChain chain) throws Throwable {
        try {
            chain.doIntercept(ctx, mainHandler);
            if (mainHandler == null) {
                ctx.render(AjaxResult.fail(404, "资源不存在"));
            }
        } catch (AppException e) {
            log.error("服务端APP运行出错", e);
            ctx.render(AjaxResult.fail(e.getMessage()));
        } catch (Throwable e) {
            log.error("服务端运行出错", e);
            ctx.render(AjaxResult.fail("服务端运行出错"));
        }
    }
}

